package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.TextView;

import com.lanlian.cayman.activity.RiskStateActivity;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.cayman.R;
import com.lanlian.uikit.MyGridView;
import com.lanlian.uikit.MyListView;
import com.lanlian.uikit.ResearchSelectAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/13 15:49
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class RiskResearchFourFragment extends BaseFragment implements View.OnClickListener {
    private TextView mTvPrevious;

    private GridView mGridView;
    private TextView mTvFinish;
    private MyGridView mGridView2;
    private MyGridView mGridView3;
    private MyGridView mGridView4;
    private MyListView mGridView5;
    private MyListView mGridView6;

    /**
     * 问题
     */
    ArrayList<String> quest = new ArrayList<>();
    private OnNextClickListener mOnNextClickListener;

    public interface OnNextClickListener {
        void onNextClick();

        void onPreviousClick();
    }

    public RiskResearchFourFragment(OnNextClickListener onNextClickListener) {
        this.mOnNextClickListener = onNextClickListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_research_four, null);
        initView(view);
        initData();
        setListeners();
        setAdapter();
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mGridView = view.findViewById(R.id.grid_view);
        mTvFinish = view.findViewById(R.id.tv_finish);
        mTvPrevious = view.findViewById(R.id.tv_previous);
        mGridView2 = view.findViewById(R.id.grid_view2);
        mGridView3 = view.findViewById(R.id.grid_view3);
        mGridView4 = view.findViewById(R.id.grid_view4);
        mGridView5 = view.findViewById(R.id.grid_view5);
        mGridView6 = view.findViewById(R.id.grid_view6);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvFinish.setOnClickListener(this);
        mTvPrevious.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        quest = new ArrayList<>();
        quest.add("1、您的年龄是？");
        quest.add("2、您在未来三个月是否急用到现金的情况？");
        quest.add("3、您在投资中配置的流动资产（高流通）占比是？");
        quest.add("4、您能够承受多大程度的亏损？");
        quest.add("5、以下哪一句最能准确描述您的工作/收入情况？");
        quest.add("6、以下哪一句最能准确描述您对投资价值波动的感受和态度");
    }

    public ArrayList<JSONObject> getData() {
        ArrayList<JSONObject> list = null;
        try {
            list = new ArrayList<>();

            JSONObject jo = new JSONObject();
            jo.put("topicId", "19");
            jo.put("topicTitle", quest.get(0).substring(2));
            jo.put("topicAnswerDesc", list_mudi.get(mGridView.getCheckedItemPosition()));
            jo.put("supplementText", "");
            if(mGridView.getCheckedItemPosition()==3)
            {
                jo.put("topicAnswerId", 86 + "");
            }
            else
            {
                jo.put("topicAnswerId", mGridView.getCheckedItemPosition() + 65 + "");
            }

            JSONObject jo2 = new JSONObject();
            jo2.put("topicId", "20");
            jo2.put("topicAnswerId", mGridView2.getCheckedItemPosition() + 68 + "");
            jo2.put("topicTitle", quest.get(1).substring(2));
            jo2.put("topicAnswerDesc", list2.get(mGridView2.getCheckedItemPosition()));
            jo2.put("supplementText", "");

            JSONObject jo3 = new JSONObject();
            jo3.put("topicId", "21");
            jo3.put("topicAnswerId", mGridView3.getCheckedItemPosition() + 70 + "");
            jo3.put("topicTitle", quest.get(2).substring(2));
            jo3.put("topicAnswerDesc", list3.get(mGridView3.getCheckedItemPosition()));
            jo3.put("supplementText", "");

            JSONObject jo4 = new JSONObject();
            jo4.put("topicId", "22");
            jo4.put("topicAnswerId", mGridView4.getCheckedItemPosition() + 74 + "");
            jo4.put("topicTitle", quest.get(3).substring(2));
            jo4.put("topicAnswerDesc", list4.get(mGridView4.getCheckedItemPosition()));
            jo4.put("supplementText", "");

            JSONObject jo5 = new JSONObject();
            jo5.put("topicId", "23");
            jo5.put("topicAnswerId", mGridView5.getCheckedItemPosition() + 78 + "");
            jo5.put("topicTitle", quest.get(4).substring(2));
            jo5.put("topicAnswerDesc", list5.get(mGridView5.getCheckedItemPosition()));
            jo5.put("supplementText", "");

            JSONObject jo6 = new JSONObject();
            jo6.put("topicId", "24");
            jo6.put("topicAnswerId", mGridView6.getCheckedItemPosition() + 82 + "");
            jo6.put("topicTitle", quest.get(5).substring(2));
            jo6.put("topicAnswerDesc", list6.get(mGridView6.getCheckedItemPosition()));
            jo6.put("supplementText", "");
            list.add(jo);
            list.add(jo2);
            list.add(jo3);
            list.add(jo4);
            list.add(jo5);
            list.add(jo6);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return list;
    }

    ArrayList<String> list_mudi;
    ArrayList<String> list2;
    ArrayList<String> list3;
    ArrayList<String> list4;
    ArrayList<String> list5;
    ArrayList<String> list6;

    private void setAdapter() {
        list_mudi = new ArrayList<>();
        list_mudi.add(">65岁");
        list_mudi.add("46~64岁");
        list_mudi.add("25~45岁");
        list_mudi.add("<25岁");
        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        list2 = new ArrayList<>();
        list2.add("是");
        list2.add("否");
        mGridView2.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView2.setAdapter(new ResearchSelectAdapter(getActivity(), list2));

        list3 = new ArrayList<>();
        list3.add("<10%");
        list3.add("10~25%");
        list3.add("26~50%");
        list3.add(">50%");
        mGridView3.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView3.setAdapter(new ResearchSelectAdapter(getActivity(), list3));

        list4 = new ArrayList<>();
        list4.add("<15%");
        list4.add("15~25%");
        list4.add("26~50%");
        list4.add(">50%");
        mGridView4.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView4.setAdapter(new ResearchSelectAdapter(getActivity(), list4));

        list5 = new ArrayList<>();
        list5.add("我已退休/没有工作且我需要依赖我的投资来支付生活费用");
        list5.add("我已退休/没有工作且我不需要依赖我的投资来支付生活费用");
        list5.add("我仍然工作且我需要依赖我的投资来增加收入");
        list5.add("我仍然工作且我不需要依赖我的投资来增加收入");
        mGridView5.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView5.setAdapter(new ResearchSelectAdapter(getActivity(), list5));

        list6 = new ArrayList<>();
        list6.add("我不希望看到投资价值出现波动，我无法承受任何资金损失");
        list6.add("我比较谨慎，到那时能接受我的投资价值出现轻微波动以及可能损失部分本金，比起最大限度的实现资本增值（10%以下），我更倾向保本和赚取收益");
        list6.add("我对投资的态度比较温和，并接受高回报（在10~25%之间），意味着必须接受我的投资价值出现波动以及可能损失本金");

        list6.add("我主要目标是实现长期资产增值（25%以上），虽然我会担心价值剧烈波动、担心损失风险提高，但是我接受相当程度的投资波动");
        mGridView6.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView6.setAdapter(new ResearchSelectAdapter(getActivity(), list6));
    }

    /**
     * 返回null 全部填写 否则返回错误提示
     *
     * @return
     */
    private String checkSelect() {
        String s1 = "您未回答问题:";
        if (mGridView.getCheckedItemPosition() == -1) {
            return s1 + quest.get(0);
        } else if (mGridView2.getCheckedItemPosition() == -1) {
            return s1 + quest.get(1);
        } else if (mGridView3.getCheckedItemPosition() == -1) {
            return s1 + quest.get(2);
        } else if (mGridView4.getCheckedItemPosition() == -1) {
            return s1 + quest.get(3);
        } else if (mGridView5.getCheckedItemPosition() == -1) {
            return s1 + quest.get(4);
        } else if (mGridView6.getCheckedItemPosition() == -1) {
            return s1 + quest.get(5);
        }
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_finish:
                RiskStateActivity act = (RiskStateActivity) getActivity();
                RiskStateActivity fa = (RiskStateActivity) getActivity();
                String msg = checkSelect();
                if (msg != null) {
                    fa.showToast(msg);
                    return;
                }
                act.tijiao();
                break;
            case R.id.tv_previous:
                mOnNextClickListener.onPreviousClick();
                break;
            default:
                break;
        }
    }

}
